using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Baik.Interface.Execution;
using Baik.Interface.Expression;
using Baik.Interface.Token;

namespace Baik.Expression
{
    public class Assignment : IAssigment
    {
        public Assignment(IExceptionThrower thrower)
        {
            Thrower = thrower;
        }

        public IExceptionThrower Thrower { get; private set; }
        public string AsString()
        {
            return  Reference.AsString() + " : " + Value.AsString();
        }
        public IObject Evaluate()
        {
            var rf = (Reference as IReference);
            if (rf == null) Thrower.Throw("Assignments Must have a reference as the left opperand");
            var rtn = Value.Evaluate(EvaluationScope);
            var host = EvaluationScope;
            //we need to find the actual object referenced in the compound reference x.y.z: would need to reslove the object returne dof x.y
            if (rf.Host != null) host = rf.Host.Evaluate(EvaluationScope);
            host.Assign(rf.Name, rtn);
            return rtn;
        }

        
        public IScope EvaluationScope { get; set; }
        public IObject Evaluate(IScope scope)
        {
            EvaluationScope = scope;
            return Evaluate();
        }
        public ILocation Location { get; set; }
        public ICallStack CallStack { get; set; }
        public IExpression Reference { get; set; }
        public IExpression Value { get; set; }
        public IEnumerable<IExpression> Params { get; set; }
    }
}